package com.asset.interceptor;

import com.asset.constants.AppConstant;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by gongshaobo on 2017/1/13  13:27.
 */
public class LoginInterceptor extends AbstractInterceptor {

    @Override
    public String intercept(ActionInvocation invoker) throws Exception {
        // TODO Auto-generated method stub

        HttpSession session = ServletActionContext.getRequest().getSession();

        if(null == session || session.isNew() || null == session.getAttribute(AppConstant.LOGIN_USER)) {
            HttpServletResponse response =  ServletActionContext.getResponse();
            response.sendRedirect(AppConstant.ROOT_NAMESPACE+"/"+AppConstant.TO_LOGIN);
            return null;
        }

        return invoker.invoke();
    }
}
